package com.bangdream.michelia.view.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bangdream.michelia.R;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.tool.RoundCornerImageView;
import com.bangdream.michelia.utils.ImageLoadUtils;
import com.bangdream.michelia.utils.StausUtils;
import com.bangdream.michelia.utils.TextViewUtil;

import java.util.List;

/**
 * @date on 2018/6/18 16:29
 * @email 909732184@qq.com
 * 任务-课程的列表适配器
 */
public class TaskCurriculumAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private OnItemClickListener onItemClickListener;
    private List<CoursesBean> data;
    private int selectTextColor,selectedTextColor;
    private Drawable selectBg,selectedBg;
    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setData(List<CoursesBean> data) {
        this.data = data;
    }

    public List<CoursesBean> getData() {
        return data;
    }

    public interface OnItemClickListener{
        void onExaminationItemClick(int position);
    }
    public TaskCurriculumAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        selectTextColor = ContextCompat.getColor(context,R.color.text_color);
        selectedTextColor = ContextCompat.getColor(context,R.color.common_red);
        selectBg = ContextCompat.getDrawable(context,R.drawable.cutting_gray_line);
        selectedBg = ContextCompat.getDrawable(context,R.drawable.cutting_red_line);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(layoutInflater.inflate(R.layout.main_task_curriculum_adapter_item,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = null;
        if(holder instanceof ViewHolder){
            viewHolder = (ViewHolder) holder;
            viewHolder.position = position;

            CoursesBean bean = data.get(position);
            ImageLoadUtils.ImageLoad(context,bean.getFaceUrl(),viewHolder.ivIcon,-1);
            viewHolder.tvTitle.setText(bean.getName()+"");
            viewHolder.tvContent.setText(Html.fromHtml(bean.getIntroduction()+""));
            viewHolder.tvCollection.setText(bean.getCollectCount()+"");
            viewHolder.tvZan.setText(bean.getBuyCount()+"");
            viewHolder.tvPeopleNumber.setText(bean.getCollectCount()+"");

            if(bean.isCollect()){
                viewHolder.btnCollection.setBackground(selectedBg);
                viewHolder.tvCollection.setTextColor(selectedTextColor);
                viewHolder.ivCollection.setImageResource(R.mipmap.icon_collection2);
            }else {
                viewHolder.btnCollection.setBackground(selectBg);
                viewHolder.tvCollection.setTextColor(selectTextColor);
                viewHolder.ivCollection.setImageResource(R.mipmap.icon_collection1);
            }

            if(bean.getLearnStatus()!=null) {
                viewHolder.tvState.setVisibility(View.VISIBLE);
                viewHolder.tvState.setText(StausUtils.coursesStateToText(bean.getLearnStatus()));
            }else {
                viewHolder.tvState.setVisibility(View.GONE);
            }

        }
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private int position;
        private LinearLayout btnZan;
        private LinearLayout btnCollection;
        private TextView tvTitle;
        private TextView tvState;
        private TextView tvContent;
        private TextView tvPeopleNumber;
        private TextView tvZan;
        private TextView tvCollection;
        //点赞现在不做
        private ImageView ivZan;
        private ImageView ivCollection;
        private RoundCornerImageView ivIcon;
        public ViewHolder(View itemView) {
            super(itemView);
            btnZan = itemView.findViewById(R.id.btnZan);
            btnCollection = itemView.findViewById(R.id.btnCollection);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvState = itemView.findViewById(R.id.tvState);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvPeopleNumber = itemView.findViewById(R.id.tvPeopleNumber);
            tvZan = itemView.findViewById(R.id.tvZan);
            tvCollection = itemView.findViewById(R.id.tvCollection);
            ivZan = itemView.findViewById(R.id.ivZan);
            ivCollection = itemView.findViewById(R.id.ivCollection);
            ivIcon = itemView.findViewById(R.id.ivIcon);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onExaminationItemClick(position);
                    }
                }
            });
        }
    }
}
